import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 蒋健
 * Date: 2022-01-22
 * Time: 20:42
 */
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }

    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }

    public void push(int val) {
        if (isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
    }

    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空！");
        }
        int oldVal = this.elem[usedSize - 1];
        this.usedSize--;
        return oldVal;
    }

    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空！");
        }
        return elem[usedSize - 1];
    }

    public int search(int val) {
        int count = usedSize - 1;
        while (elem[count] != val) {
            count--;
            if (count<0){
                return -1;
            }
        }
        return usedSize-count;
    }
}
